﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Text;
using Com.Toook.Common;
using System.IO;
using System.IO.Compression;

namespace Com.Toook.ParseHtml
{
    /// <summary>
    ///Utils 公共工具类
    /// </summary>
    public class Utils
    {
        public Utils()
        {
        }
        /// <summary>
       /// 下载指定url的html源代码字符串
        /// </summary>
        /// <param name="url">页面url</param>
        /// <param name="encode">传null使用默认编码。指定时使用特殊页面编码: gzip加具体编码(如gziputf-8)/utf8/ascii/unicode </param>
        /// <returns>成功：返回html；失败：返回null</returns>
        public static string DownloadHtml(string url, string encode)
        {
            string html = null;
            if (string.IsNullOrEmpty(encode))
            {
                encode = "default";
            }

            byte[] data = null;
            try
            {
                data = new WebClient().DownloadData(url);                
            }
            catch
            {
                LogHelper.Log.Warn("下载网页html失败。WebClient().DownloadData(\"" + url + "\")");
            }
            if (data != null && data.Length > 10)
            {
                if (encode.ToLower().StartsWith("gzip"))
                {
                    html = DownloadGzip(url, encode);
                }
                else
                {
                    switch (encode.ToLower())
                    {
                        case "utf-8":
                            html = Encoding.UTF8.GetString(data);
                            break;
                        case "ascii":
                            html = Encoding.ASCII.GetString(data);
                            break;
                        case "unicode":
                            html = Encoding.Unicode.GetString(data);
                            break;
                        default:
                            html = Encoding.Default.GetString(data);
                            break;
                    }
                }
            }

            return html;
        }
        /// <summary>
        /// 下载gzip压缩页面
        /// </summary>
        /// <param name="url"></param>
        /// <param name="gzipencode"></param>
        /// <returns>成功：返回html；失败：返回null</returns>
        protected static string DownloadGzip(string url, string gzipencode)
        {
            string html = null;
            string encode = gzipencode.Substring(4).ToLower();
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 5000;
                request.AllowAutoRedirect = false;

                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                    }
                    else
                    {
                        switch (encode)
                        {
                            case "utf-8":
                                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                                break;
                            case "unicode":
                                reader = new StreamReader(response.GetResponseStream(), Encoding.Unicode);
                                break;
                            case "ascii":
                                reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
                                break;
                            default:
                                reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                                break;
                        }
                    }
                    html = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("下载gzip压缩页面出错。gzipencode=" + gzipencode + ", url=" + url, ex);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (reader != null)
                {
                    reader.Close();
                }
                if (request != null)
                {
                    request = null;
                }
            }
            return html;
        }
    }
}